package net.xdclass.controller;

import net.xdclass.controller.request.LinkGroupAddRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.service.LinkGroupService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    @PostMapping("add")
    public JsonData add (@RequestBody LinkGroupAddRequest request) {
        int rows = linkGroupService.add(request);
        return rows==1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }

    @DeleteMapping("del/{groupId}")
    public JsonData del (@PathVariable("groupId") Long groupId) {
        int rows = linkGroupService.del(groupId);
        return rows==1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    @GetMapping("detail/{groupId}")
    public JsonData detail (@PathVariable("groupId") Long groupId) {
        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    @GetMapping("list")
    public JsonData findUserAllLinkGroup () {
        List<LinkGroupVO> linkGroupVOS = linkGroupService.findUserAllLinkGroup();
        return JsonData.buildSuccess(linkGroupVOS);
    }

    @PutMapping("update")
    public JsonData update (@RequestBody LinkGroupUpdateRequest request) {
        int rows = linkGroupService.update(request);
        return rows==1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }

}

